package com.von.nhcf.util;

/**
 * 统一Ajax请求的响应格式
 * @author von
 * @param <T>
 */
public class ResultEntity <T>{
    //响应结果常量：成功和失败
    private static final String SUCCESS = "SUCCESS";
    private static final String FAILURE = "FAILURE";

    //请求结果
    private String result;
    //结果描述，用于返回失败时的错误描述信息
    private String msg;
    //结果数据
    private T data;

    /**
     * 请求成功但是没有数据
     * @param <E>
     * @return
     */
    public static <E> ResultEntity successWithoutData() {
        return new ResultEntity(SUCCESS, null, null);
    }

    /**
     * 请求成功并且有数据
     * @param data
     * @param <E>
     * @return
     */
    public static <E> ResultEntity<E> successWithData(E data) {
        return new ResultEntity<E>(SUCCESS, null, data);
    }

    public static ResultEntity failureWithMsg(String msg) {
        return new ResultEntity(FAILURE, msg, null);
    }

    public static ResultEntity failureWithoutMsg() {
        return new ResultEntity();
    }

    public static ResultEntity successWithMsg(String msg) {
        return new ResultEntity(SUCCESS, msg, null);
    }
    public ResultEntity() {
    }

    public static String getSUCCESS() {
        return SUCCESS;
    }

    public static String getFAILURE() {
        return FAILURE;
    }
    public ResultEntity(String result, String msg, T data) {
        this.result = result;
        this.msg = msg;
        this.data = data;
    }


    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "result='" + result + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
